package binary;

public class Tree {
    public Node root;
    public static int index;


    public Node CreateTree(int[] a) {
        Node root = null;
        if (a[index] != '#') {
            root = new Node(a[index]);
            index++;
            root.setLeft(CreateTree(a));
            index++;
            root.setRight(CreateTree(a));
        }
        return root;
    }

    //先序
    public void preOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.print(root.getData() + ",");
        preOrder(root.getLeft());
        preOrder(root.getRight());
    }

    //中序
    public void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.getLeft());
        System.out.print(root.getData() + ",");
        inOrder(root.getRight());
    }

    //后序
    public void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.getLeft());
        postOrder(root.getRight());
        System.out.print(root.getData() + ",");
    }

    public static void main(String[] agrs) {
        Tree tree = new Tree();
        int[] a = new int[]{0, 1, 3,'#', 7, '#','#', 4, '#', '#', 2, 5, '#', '#',6,'#','#'};
        //int[] a = new int[]{1,2,3,'#','#',4,'#','#',5,6,'#','#','#' };
        tree.root = tree.CreateTree(a);
        System.out.println("先序遍历：");
        tree.preOrder(tree.root);
        System.out.println("\n中序遍历：");
        tree.inOrder(tree.root);
        System.out.println("\n后序遍历：");
        tree.postOrder(tree.root);
        System.out.println();

    }
}
